package com.cnunicom.modules.tra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.Constant;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;
import com.cnunicom.modules.tra.dao.ResRecordDao;
import com.cnunicom.modules.tra.entity.ResRecordEntity;
import com.cnunicom.modules.tra.service.ResRecordService;
import com.cnunicom.modules.tra.vo.ResRecMebDisVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("resRecordService")
public class ResRecordServiceImpl extends ServiceImpl<ResRecordDao, ResRecordEntity> implements ResRecordService {

    @Override
    public List<Object> queryPage(Map<String, Object> params) {
        String resName = (String)params.get("resName");
        String eatName = (String)params.get("eatName");


        IPage<ResRecordEntity> page = this.page(
                new Query<ResRecordEntity>().getPage(params),
                new QueryWrapper<ResRecordEntity>()
                        .like(StringUtils.isNotBlank(resName),"res_name", resName)
                        .like(StringUtils.isNotBlank(eatName),"eat_name", eatName)
        );

        BigDecimal totalPrice = page.getRecords().stream()
                .map(ResRecordEntity::getPrice)
                .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);

        List<Object> objects = new ArrayList<>();
        objects.add(new PageUtils(page));
        objects.add(totalPrice);

        return objects;
    }

    /**
     * 手写查询
     *
     * @param params 查询参数
     * @return 封装page类
     */
    @Override
    public List<Object> queryResRecMebDisPage(Map<String, Object> params) {
        ResRecMebDisVO vo = voHandler(params);

        String resName = (String) params.get("resName");
        String eatName = (String) params.get("eatName");

        IPage<ResRecMebDisVO> page = new Query<ResRecMebDisVO>().getPage(params, "create_time", false);
        List<ResRecMebDisVO> vos = this.baseMapper.queryResRecMebDisPage((Page) params.get(Constant.PAGE), vo);

        BigDecimal totalPrice = getTotalPrice(resName, eatName, vos);

        page.setRecords(vos);
        PageUtils pageUtils = new PageUtils(page);

        List<Object> objects = new ArrayList<>();
        objects.add(pageUtils);
        objects.add(totalPrice);

        return objects;
    }




    /**
     * 两个查询条件，最大三种查询方式
     *    1.只查询餐厅
     *    2.只查询就餐方式
     *    3.餐厅和就餐方式共查
     * @param resName 餐厅名称
     * @param eatName 就餐方式
     * @return 统计金额
     */
    private BigDecimal getTotalPrice(String resName,String eatName,List<ResRecMebDisVO> vos) {

        BigDecimal result = BigDecimal.ZERO;

        //无查询条件
        if (StringUtils.isBlank(resName) && StringUtils.isBlank(eatName)) {
            result = vos.stream().map(ResRecMebDisVO::getPrice).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        }

        // 只查询餐厅
        if (StringUtils.isNotBlank(resName) && StringUtils.isBlank(eatName)) {
            result = vos.stream().filter(item -> item.getResName().equals(resName))
                    .map(ResRecMebDisVO::getPrice)
                    .reduce(BigDecimal::add)
                    .orElse(BigDecimal.ZERO);
        }
        // 只查询就餐方式
        if(StringUtils.isNotBlank(eatName) && StringUtils.isBlank(resName)){
            result = vos.stream().filter(item -> item.getEatName().equals(eatName))
                    .map(ResRecMebDisVO::getPrice)
                    .reduce(BigDecimal::add)
                    .orElse(BigDecimal.ZERO);
        }
        // 共查
        if (StringUtils.isNotBlank(resName) && StringUtils.isNotBlank(eatName)) {
            result = vos.stream().filter(item -> item.getResName().equals(resName) && item.getEatName().equals(eatName))
                    .map(ResRecMebDisVO::getPrice)
                    .reduce(BigDecimal::add)
                    .orElse(BigDecimal.ZERO);
        }
        return result;
    }

    /**
     * vo拼接
     * @param params 查询参数
     * @return 拼接完成的vo
     */
    private ResRecMebDisVO voHandler(Map<String, Object> params) {
        ResRecMebDisVO vo = new ResRecMebDisVO();
        String resName = (String) params.get("resName");
        String eatName = (String) params.get("eatName");

        vo.setResName(resName);
        vo.setEatName(eatName);

        return vo;

    }

    /*@Override
    public ResRecMebDisVO getById(Integer id) {
        return this.baseMapper.getById(id);
    }*/
}
